package com.broad.commerce.code.base;

import com.broad.commerce.code.modules.BaseModule;
import com.broad.commerce.common.utils.core.StringUtils;
import org.joda.time.DateTime;

import java.util.List;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-02-07 16:43
 * @description: AbstractJavaService
 **/
public abstract class AbstractJavaService extends AbstractBaseService {

	/**
	 * Create entity code string.
	 * @param baseModule the base module
	 * @return the string
	 */
	@Override
	public String createClassCode(BaseModule baseModule) {
		StringBuilder sb = new StringBuilder();
		//包名
		this.createPackageCode(sb, baseModule);
		//导入类
		this.createImportCode(sb, baseModule);
		//注释
		this.createNotationCode(sb, baseModule);
		//注解
		this.createAnnotationCode(sb, baseModule);
		//类头部
		this.createClassStartTag(sb, baseModule);
		//类内容
		this.createClassContent(sb, baseModule);
		//类结束标签
		this.createClassEndTag(sb);
		return sb.toString();
	}

	/**
	 * Create class file.
	 * @param baseModule the base module
	 * @param rootPath the root path
	 * @param fileType the file type
	 */
	@Override
	public void createClassFile(BaseModule baseModule, String filePath, String fileType) {
		super.createClassFile(baseModule, filePath, fileType);
	}

	/**
	 * Create batch class files.
	 * @param baseModules the base modules
	 * @param rootPath the root path
	 * @param fileType
	 */
	@Override
	public void batchCreateClassFiles(List<BaseModule> baseModules, String filePath, String fileType) {
		super.batchCreateClassFiles(baseModules, filePath, fileType);
	}

	/**
	 * Create package code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	protected void createPackageCode(StringBuilder sb, BaseModule baseModule) {
		sb.append("package ").append(baseModule.getPackageName()).append(";\n").append("\n");
	}

	/**
	 * Create import code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	protected abstract void createImportCode(StringBuilder sb, BaseModule baseModule);

	/**
	 * Create notation code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	@SuppressWarnings("DuplicatedCode")
	protected void createNotationCode(StringBuilder sb, BaseModule baseModule) {
		String author = StringUtils.isNotEmpty(baseModule.getAuthor()) ? baseModule.getAuthor() : "Stephen";
		String comment = StringUtils.isNotEmpty(baseModule.getComment()) ? baseModule.getComment() : baseModule.getClassName();
		//@off
		sb.append("/**").append("\n")
		  .append("* @author ").append(author).append("\n")
	      .append("* @version V1.0").append("\n")
		  .append("* @date ").append(new DateTime().toString("yyyy-MM-dd HH:mm")).append("\n")
		  .append("* @description: ").append(comment).append("\n")
		  .append("**/").append("\n");//@on
	}

	/**
	 * Create annotation code.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	protected abstract void createAnnotationCode(StringBuilder sb, BaseModule baseModule);

	/**
	 * Create class start tag.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	protected abstract void createClassStartTag(StringBuilder sb, BaseModule baseModule);

	/**
	 * Create class content.
	 * @param sb the sb
	 * @param baseModule the base module
	 */
	protected abstract void createClassContent(StringBuilder sb, BaseModule baseModule);

	protected void createClassEndTag(StringBuilder sb) {
		sb.append("}").append("\n");
	}

	@Override
	public String createXmlCode(BaseModule baseModule) {
		return null;
	}
}
